//
//  SINetworkTools+Extension.swift
//  SinaImiate
//
//  Created by xiaoming on 16/7/2.
//  Copyright © 2016年 GoodJob. All rights reserved.
//

import Foundation

extension SINetworkTools {
    
    func statusList(since_id: Int64 = 0, max_id: Int64 = 0, completion:(list:[[String: AnyObject]]?, isSuccess: Bool) -> ()) {
        
        let urlString = "https://api.weibo.com/2/statuses/home_timeline.json"
        
        let params = ["since_id": "\(since_id)", "max_id": "\(max_id > 0 ? max_id - 1 : 0)"]
        
        
        SINetworkTools.shared.tokenRequests(URLString: urlString, parameters: params) { (json, isSuccess) in
//            print(json)
            
            let result = json?["statuses"] as? [[String: AnyObject]]
            
            completion(list: result, isSuccess: isSuccess)
            
        }
    }
    
    //MARK: -未阅读的微博数量 
    func unreadCount(completion:(count: Int) -> ()) {
        
        guard let uid = userAccount.uid else {
            
            return
        }
        
        let urlString = "https://rm.api.weibo.com/2/remind/unread_count.json"
        
        let params = ["uid": uid]
        
        tokenRequests(URLString: urlString, parameters: params) { (json, isSuccess) in
            
            let dict = json as? [String: AnyObject]
            
            let count = dict?["status"] as? Int
            
            completion(count: count ?? 0)
            
        }
    }
}

//MARK: 发布微博
extension SINetworkTools {
    
    func postStatus(text: String,image: UIImage?, completion: (result: [String: AnyObject]?, isSuccess: Bool) -> ()) {
        
        let urlString: String
        
        if image == nil {
            
            urlString = "https://api.weibo.com/2/statuses/update.json"
        } else {
            urlString = "https://upload.api.weibo.com/2/statuses/upload.json"
        }
        
        let params = ["status": text]
        
        var name: String?
        var data: Data?
        
        if image != nil {
            name = "pic"
            data = UIImagePNGRepresentation(image!)
        }
        
        tokenRequests(method: .POST, URLString: urlString, parameters: params, name: name, data: data) { (json, isSuccess) in
            
            completion(result: json as? [String: AnyObject], isSuccess: isSuccess)
        }
        
        
    }
    
}

extension SINetworkTools {
    
    /// 获取令牌
    ///
    func loadAccessToken(code: String, completion: (isSuccess: Bool) -> ()) {
        
        let urlString = "https://api.weibo.com/oauth2/access_token"
        
        let params = ["client_id": SIAppleKey,
                      "client_secret": SIAppSecret,
                      "grant_type": "authorization_code",
                      "code": code,
                      "redirect_uri": SIRedirectURI]
        
        request(method: .POST, URLString: urlString, parameters: params) { (json, isSuccess) in

            self.userAccount.yy_modelSet(with: (json as? [String: AnyObject] ?? [:]))
            
            self.loadUserInfo(completion: { (dict) in
                self.userAccount.yy_modelSet(with: dict)
                print(self.userAccount)
                //FIXME: - 保存模型到磁盘
                self.userAccount.saveAccount()
                
                completion(isSuccess: isSuccess)
            })
            
            
            
        }
        
    }
}

extension SINetworkTools {
    
    ///加载用户信息,用户登陆后立即执行
    func loadUserInfo(completion: (dict: [String: AnyObject]) -> ()) {
        
        guard let uid = userAccount.uid else {
            return
        }
        
        let urlString = "https://api.weibo.com/2/users/show.json"
        
        let params = ["uid": uid]
        
        tokenRequests(URLString: urlString, parameters: params) { (json, isSuccess) in
            ///完成回调,拿到用户数据
            completion(dict: (json as? [String: AnyObject]) ?? [:])
        }
        
        
    }
    
}
